using System;
using System.Collections.Generic;
using System.IO;
using Comdiv.Extensions;
using MvcContrib.Comdiv.ViewEngines.Brail;

namespace Comdiv.Doc{
    public abstract class DocumentGeneratorBrailBase: BrailBase{
        public DocumentGeneratorBrailBase(BooViewEngine viewEngine) : base(viewEngine){
            
        }
        protected string nsfile(string ns){
            var sw = new StringWriter();
            OutputSubView("ns_file_name", sw, new Dictionary<string, object>{{"myns", ns}});
            return Path.GetFileNameWithoutExtension(sw.ToString());
        }
        protected string typefile(TypeDescriptor td)
        {
            var sw = new StringWriter();
            OutputSubView("type_file_name", sw, new Dictionary<string, object> { { "mytype", td } });
            return Path.GetFileNameWithoutExtension(sw.ToString());
        }

        protected string wtlink(TypeDescriptor td){
            return wtlink(td, false);
        }

        protected DocumentQuery _query{
            get{

                return Properties["query"] as DocumentQuery;
            }
        }


        protected string wnslink(string ns)
        {
            var fname = nsfile(ns);
            return "[{0} {1}]"._format(fname, ns);
        }

        protected string wtlink(Type type){
            return wtlink(type, false);
        }

        protected string wtlink(Type type, bool fullName)
        {
            if (null == type) return "";
            var td = _query[type];
            if (null == td) return fullName ? type.FullName : type.Name;
            return wtlink(td, fullName);
        }

        protected string wtlink(TypeDescriptor td, bool fullName)
        {
            var fname = typefile(td);
            return "[{0} {1}]"._format(fname, fullName? td.Type.FullName:td.Type.Name);
        }
        
    }
}